package com.suyang.syt.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.WeixinGroup;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.WeixinGroupService;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/weixin/group")
@AuthClass(value = "login")
public class WeixinGroupController {

	@Autowired
	private WeixinGroupService weixinGroupService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@AuthMethod(role = "ROLE_WEIXIN")
	public String list() {
		return "weixin/group/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public PagerEasyUI<WeixinGroup> list(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {
			return weixinGroupService.listByEasyUI(page, rows, sort, order);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/listAll", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public List<WeixinGroup> listAll() {
		try {
			return weixinGroupService.listAll();
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, String name) {
		try {
			return weixinGroupService.add(name);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/sync", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj sync(HttpServletRequest request) {
		try {
			return weixinGroupService.updateSync();
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/deletes", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj deletes(HttpServletRequest request, String ids) {
		try {
			return weixinGroupService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, int id, String name) {
		try {
			return weixinGroupService.update(id, name);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
